/**
 * com.monte.persistence.command.MyBatisBatchInsertCommand.java
 * add by FengMy
 */
package com.monte.persistence.command;

import java.util.Collection;

import org.apache.ibatis.session.SqlSession;

import com.monte.persistence.CommandContext;
import com.monte.persistence.PersistenceCommand;
import com.monte.util.StringUtils;

/**
 * 描述：
 * @author FengMy
 * @since 2013-8-31 
 */
public class MyBatisBatchInsertCommand<T> implements PersistenceCommand {
	final private String mapper;
	final private Collection<T> entitys;
	public MyBatisBatchInsertCommand(String mapper,Collection<T> entitys){
		if(StringUtils.isEmpty(mapper, true) || entitys==null){
			throw new RuntimeException("mapper and entitys could not be null.");
		}
		this.mapper = mapper;
		this.entitys = entitys;
	}
	@Override
	public Object execute(CommandContext context) {
		SqlSession session = context.getSqlSession();
		for(T t : entitys){
			session.insert(mapper, t);
		}
		return entitys.size();
	}
}
